package com.csjy.migration.hnnd.business;

import com.csjy.migration.hnnd.BusinessTool;
import com.csjy.migration.hnnd.DbUtil;
import com.csjy.migration.hnnd.ProgramArgument;

import com.csjy.migration.hnnd.annotation.BusinessDataConverter;

import javax.script.ScriptException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 *  避嫌和移交记录(避嫌)
 */
@BusinessDataConverter(
        insertSql = "insert into avoiding_employee_information (ID, CREATE_NAME, CREATE_BY, CREATE_DATE, UPDATE_NAME, UPDATE_BY, UPDATE_DATE, SYS_ORG_CODE, SYS_COMPANY_CODE, BPM_STATUS, PRO_ID, ALLOW_FLAG, STAFF_NAME, STAFF_ID)"  +
                "values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
        updateSql = "UPDATE avoiding_employee_information SET CREATE_NAME = ?,CREATE_BY = ?,CREATE_DATE = ?,UPDATE_NAME = ?,UPDATE_BY = ?,UPDATE_DATE = ?,SYS_ORG_CODE = ?,SYS_COMPANY_CODE = ?,BPM_STATUS = ?,PRO_ID = ?,ALLOW_FLAG = ?,STAFF_NAME = ?,STAFF_ID = ? WHERE ID = ?",
        findMigratedIdListSql = "SELECT ID FROM avoiding_employee_information WHERE PRO_ID = ?"
)
public class AvoidingEmployeeInformation extends AbstractBusiness {

    public AvoidingEmployeeInformation(DbUtil sourceDbUtil, DbUtil targetDbUtil, SourceBusinessData sourceData, ProgramArgument programArgument, String projectId) throws SQLException {
        super(sourceDbUtil, targetDbUtil, sourceData, programArgument, projectId);
    }


    @Override
    public void migrate() throws ScriptException, SQLException, Exception {

        List<Map> BtItemArouseSecurityList = sourceData.getBtItemArouseSecurity();
        for (Map btItemArouseSecurity : BtItemArouseSecurityList) {
            try {
                String sourceId = getSimpleId(btItemArouseSecurity.get("CREATE_TIME").toString(), btItemArouseSecurity.get("ITEMNO").toString());
                int needMigratedResult = this.needMigrated(sourceId);
                if (needMigratedResult == 0) {
                    continue;
                };
                // 单条插入或更新
                if (needMigratedResult == 1) {
                    this.targetDbUtil.updateByParams(updateSql, this.buildValues(btItemArouseSecurity, sourceId), true);
                    // 批量插入或更新 和 单条插入或更新 二选一
//             this.batchUpdateValues.add(this.buildValues(row));
                } else {
                    this.targetDbUtil.updateByParams(insertSql, this.buildValues(btItemArouseSecurity, sourceId));
                    // 批量插入或更新 和 单条插入或更新 二选一
//             this.batchInsertValues.add(this.buildValues(row));
                }

            } catch (Exception e) {
                throw new Exception(String.format("避嫌和移交记录(避嫌)[%s]迁移失败:%s\n", btItemArouseSecurity.get("ITEMNO"), e.getMessage()));
            }
        }
   
        }
        public List buildValues (Map btItemArouseSecurity, String id ) throws SQLException, ScriptException {
            List values = new ArrayList();
            //ID
            values.add(id);
            //CREATE_NAME	创建人名称
            values.add(null);
            //CREATE_BY	创建人登录名称
            values.add(null);
            //CREATE_DATE	创建日期
            values.add(btItemArouseSecurity.get("CREATE_TIME"));
            //UPDATE_NAME	更新人名称
            values.add(null);
            //UPDATE_BY	更新人登录名称
            values.add(null);
            //UPDATE_DATE	更新日期
            values.add(btItemArouseSecurity.get("MODIFY_TIME"));
            //SYS_ORG_CODE	所属部门
            values.add(null);
            //SYS_COMPANY_CODE	所属公司
            values.add(null);
            //BPM_STATUS	流程状态
            values.add(1);
            //PRO_ID	项目ID
            values.add(btItemArouseSecurity.get("ITEMNO"));
            //ALLOW_FLAG	是否容许查看项目信息
            int AllowFlag = 1;
            if (btItemArouseSecurity.get("EXTFLAG").toString().equals("1")) {
                AllowFlag = 0;
            }
            values.add(AllowFlag);
            //STAFF_NAME	员工姓名
            values.add(BusinessTool.getEmpNameByEmpCode(btItemArouseSecurity.get("EMPCODE")));
            //STAFF_ID	员工ID
            values.add(btItemArouseSecurity.get("EMPCODE"));
            return values;
        }
    }


